package com.tech.koufu.ui.adapter;

import java.util.List;

import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;

public class HomeBannersAdapter extends PagerAdapter {

	private List<View> views = null;
	private float rate = 1;

	public HomeBannersAdapter(List<View> views) {
		this.views = views;
	}

	public HomeBannersAdapter(List<View> views, float rate) {
		this.views = views;
		this.rate = rate;
	}

	@Override
	public void destroyItem(View arg0, int arg1, Object arg2) {
//		if(arg1<getCount()&& arg1>=0)
			((ViewPager) arg0).removeView(views.get(arg1));
	}

	@Override
	public void finishUpdate(View arg0) {
	}

	@Override
	public int getCount() {
		return views.size();
	}

	@Override
	public Object instantiateItem(View arg0, int arg1) {
		View child = null;
		try {
			 child = views.get(arg1);
			ViewGroup vp =  (ViewGroup) child.getParent();
			if(vp!=null)
				vp.removeView(child);  
			((ViewPager) arg0).addView(child, 0);
		} catch (Exception e) { 
			e.printStackTrace();
		}
		return child;
	}

	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		return arg0 == arg1;
	}

	@Override
	public void restoreState(Parcelable arg0, ClassLoader arg1) {
	}

	@Override
	public Parcelable saveState() {
		return null;
	}

	@Override
	public void startUpdate(View arg0) {
	}

	public float getPageWidth(int position) {
		return rate;
	}

	public List<View> getViews() {
		return views;
	}

	public void setViews(List<View> views) {
		this.views = views;
	}

}
